///<reference path="../common/vscodeapi.d.ts"/>



//用拼串大法合并
let plugin_path = "http://127.0.0.1:8000/";
let edit_filename = "1.atlas.json";

let ws_addr = "ws://127.0.0.1:8000/ws";

//这个TS 是特殊的，不要引用他，他是动态拼合的外挂代码



//下面是固定代码


class Api_Impl implements VsCodeApi {
    constructor(ws_addr: string) {
        this._ws = new WebSocket(ws_addr, "ws");
        this._ws.onopen = (e) => {
            console.warn("websocket in.");
        }
        this._ws.onerror = (e => {
            console.warn("websocket error");
        }
        );
        this._ws.onclose = (e) => {
            console.warn("websocket colse.");
            window.close();
        }
        this._ws.onmessage = (e) => {
            console.log("websocket recv:" + e.data);
            let msg = JSON.parse(e.data);
            let cmd = msg["cmd"] as string;
            if (cmd == "state") {
                this._state = msg["data"];
                console.log("recv update state.");
                return;
            }



            if (window.onmessage != undefined) {
                let data = { "data": msg };
                (window.onmessage as any)(data);
            }
        }
    }
    private _ws: WebSocket;
    private _state: any;
    postMessage(msg: any): void {
        this._ws.send(JSON.stringify(msg));
    }

    getState(): any {
        return this._state;
    }
    setState(state: any): void {
        this._state = state;
        let send = { "cmd": "state", "data": state };
        this._ws.send(JSON.stringify(send));
    }
}

let __hide_api = new Api_Impl(ws_addr);




function acquireVsCodeApi(): VsCodeApi {
    return __hide_api;
}

function getVsCodeEditFileName(): string {
    return edit_filename;
}
function getVsCodePluginPath(): string {
    return plugin_path;
}
